<?php
App::uses('AppController', 'Controller');
/**
 * Submenus Controller
 *
 * @property Submenu $Submenu
 */
class SubmenusController extends AppController {


/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Submenu->recursive = 0;
		$this->set('submenus', $this->paginate());
	}

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Submenu->id = $id;
		if (!$this->Submenu->exists()) {
			throw new NotFoundException(__('Invalid submenu'));
		}
		$this->set('submenu', $this->Submenu->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Submenu->create();
			if ($this->Submenu->save($this->request->data)) {
				$this->Session->setFlash(__('The submenu has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The submenu could not be saved. Please, try again.'));
			}
		}
		$domains = $this->Submenu->Domain->find('list');
		$this->set(compact('domains'));
	}

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Submenu->id = $id;
		if (!$this->Submenu->exists()) {
			throw new NotFoundException(__('Invalid submenu'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Submenu->save($this->request->data)) {
				$this->Session->setFlash(__('The submenu has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The submenu could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Submenu->read(null, $id);
		}
		$menuses = $this->Submenu->Domain->find('list');
		$this->set(compact('menuses'));
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Submenu->id = $id;
		if (!$this->Submenu->exists()) {
			throw new NotFoundException(__('Invalid submenu'));
		}
		if ($this->Submenu->delete()) {
			$this->Session->setFlash(__('Submenu deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Submenu was not deleted'));
		$this->redirect(array('action' => 'index'));
	}

	
/**
* Rebuild all the left and right fields based on the parent_id
* $this->Category->recover();
* or
* $this->Category->recover('parent');
* Rebuild all the parent_id's based on the lft and rght fields
* $this->Category->recover('tree');
*
*/
	public function recover(){
		$this->Submenu->recover();
		$this->redirect($this->referer());
	}	
}
